package upcweb.web;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import upcweb.exception.DAOExcepcion;
import upcweb.modelo.ClsServicio;
import upcweb.negocio.BsGestionServicio;
import upcweb.negocio.BsTipoServicio;


//Para subir el Archivo : 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;


@Controller
public class ServicioController {

	@RequestMapping(value = "/servicios", method = RequestMethod.POST) 
	protected ModelAndView goServicios(HttpServletRequest request,
			HttpServletResponse response) throws DAOExcepcion{
		
		ModelAndView MAV=new ModelAndView();
		MAV.setViewName("servicios/mvServicio");
		
		/*BsGestionServicio oBSGServicio= new BsGestionServicio();
		ArrayList<ClsServicio> lstServicio= oBSGServicio.listar();*/
		BsTipoServicio oBsTipoServicio= new BsTipoServicio();
		MAV.addObject("lstTipoServicio",oBsTipoServicio.ListarTipoServicio_Todo());	
		String parametros[]= new String[2];
		parametros[0]= "";
		parametros[1]= "2";		
		BsGestionServicio oBSGServicio= new BsGestionServicio();
		ArrayList<ClsServicio> lstServicio= oBSGServicio.listarxFiltro(parametros);
		MAV.addObject("lstServicio",lstServicio);
		MAV.addObject("Editar","0");
		return  MAV;
	}
	
	@RequestMapping(value = "/serviciosedit", method = RequestMethod.POST) 
	protected ModelAndView goServicioEdit(HttpServletRequest request,
			HttpServletResponse response) throws DAOExcepcion{
		
		ModelAndView MAV=new ModelAndView();
		BsGestionServicio oBSGServicio= new BsGestionServicio();
		BsTipoServicio oBsTipoServicio= new BsTipoServicio();
		
		MAV.setViewName("servicios/mvServicioForm");
		ClsServicio oClsServicio= oBSGServicio.buscar(Integer.parseInt(request.getParameter("id")));
		MAV.addObject("lstTipoServicio",oBsTipoServicio.ListarTipoServicio_Todo());			
		MAV.addObject("oClsServicio",oClsServicio);		
		MAV.addObject("Editar","1");
		return  MAV;
	}
	
	@RequestMapping(value = "/serviciosave", method = RequestMethod.POST) 
	protected ModelAndView goServicioSave(@RequestParam("tx_Img") MultipartFile file, WebRequest webRequest,HttpServletRequest request, Model model) throws DAOExcepcion, IOException{
		
		ModelAndView MAV=new ModelAndView();
		BsGestionServicio oBSGServicio= new BsGestionServicio();
		int iTipo = Integer.parseInt(webRequest.getParameter("hidTipo"));
		ClsServicio oClsServicio=null;
		if(iTipo==1){
			oClsServicio=oBSGServicio.buscar(Integer.parseInt(webRequest.getParameter("accountusername")));
		}else{
			oClsServicio=new ClsServicio();
			oClsServicio.setIdService(Integer.parseInt(webRequest.getParameter("accountusername")));
		}
				
		String oFileName= file.getOriginalFilename();		
		
		InputStream inputStream=null;
		OutputStream outputStream=null;
		String realPath="";
		if(file.getSize() > 0){
			inputStream=file.getInputStream();	
		    realPath=request.getServletContext().getRealPath("") + "\\imgservicio\\dir\\" + file.getOriginalFilename();
			System.out.println("--> " + webRequest.getContextPath());
			System.out.println("otro --> " + realPath);
			outputStream = new FileOutputStream(realPath);
			int readBytes = 0;
			byte[] buffer = new byte[10000];
			
			while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
                outputStream.write(buffer, 0, readBytes);
        }
        outputStream.close();
        inputStream.close();		
			
		}
		

		   	oClsServicio.setIdTypeService(Integer.parseInt(webRequest.getParameter("ddlTipoServicio")));
	     	oClsServicio.setState( (Integer.parseInt(webRequest.getParameter("ddlEstadof"))==1)? true:false  );
			oClsServicio.setCostPerPerson(Double.parseDouble(webRequest.getParameter("tx_CostoPersona")));
			oClsServicio.setDescription(webRequest.getParameter("tx_Descripcion"));
			if(!oFileName.isEmpty() && oFileName.length()>4){
				oClsServicio.setImageURL("/dir/" + oFileName);
			}
			
			oClsServicio.setName(webRequest.getParameter("tx_Nombre"));
			oClsServicio.setMinParticipants(Integer.parseInt(webRequest.getParameter("tx_Cantidad")));
		   
			if(iTipo==1){
				boolean flGuardar= oBSGServicio.actualizar(oClsServicio);
			}else{
				oClsServicio= oBSGServicio.insertar(oClsServicio);
			}
							
			
		MAV.addObject("lstServicio",oBSGServicio.listar());	
	//	MAV.addObject("Editar","0");
		MAV.setViewName("redirect:index.html#servicios.html");
		return  MAV;
	}

	@RequestMapping(value = "/viewdetailserv", method = RequestMethod.POST) 
	protected ModelAndView viewDetails(HttpServletRequest request,
			HttpServletResponse response) throws DAOExcepcion{
		
		ModelAndView MAV=new ModelAndView();
		BsGestionServicio oBSGServicio= new BsGestionServicio();
		BsTipoServicio oBsTipoServicio= new BsTipoServicio();
		
		MAV.setViewName("servicios/mvServicioDetalle");
		ClsServicio oClsServicio= oBSGServicio.buscar(Integer.parseInt(request.getParameter("id")));
		MAV.addObject("lstTipoServicio",oBsTipoServicio.ListarTipoServicio_Todo());			
		MAV.addObject("oClsServicio",oClsServicio);			
		MAV.addObject("Editar","1");
		return  MAV;
	}

	@RequestMapping(value = "/serviciodelete", method = RequestMethod.POST) 
	protected ModelAndView viewseroDelete(HttpServletRequest request,
			HttpServletResponse response) throws DAOExcepcion{		
		ModelAndView MAV=new ModelAndView();
		BsGestionServicio oBSGServicio= new BsGestionServicio();
		BsTipoServicio oBsTipoServicio= new BsTipoServicio();		
		MAV.setViewName("servicios/mvServicio");
		boolean flEliminar= oBSGServicio.eliminar(Integer.parseInt(request.getParameter("id")));			
		MAV.addObject("Editar","0");
		return  MAV;
	}
	
	@RequestMapping(value = "/serviciostabla", method = RequestMethod.POST) 
	protected ModelAndView goServiciosTabla(HttpServletRequest request,
			HttpServletResponse response) throws DAOExcepcion{
		
		ModelAndView MAV=new ModelAndView();
		MAV.setViewName("servicios/mvServicioLista");
		
		String parametros[]= new String[2];
		parametros[0]= request.getParameter("tx_buscarnom");
		parametros[1]= request.getParameter("ddlEstado");		
		BsGestionServicio oBSGServicio= new BsGestionServicio();
		ArrayList<ClsServicio> lstServicio= oBSGServicio.listarxFiltro(parametros);
		//ArrayList<ClsServicio> lstServicio= oBSGServicio.listar();
		MAV.addObject("lstServicio",lstServicio);
		MAV.addObject("Editar","0");
		return  MAV;
	}
	
}
